package com.qczl.pme.controller.stb;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.qczl.core.controller.BaseController;
import com.qczl.model.Json;
import com.qczl.pme.entity.PMEChannel;
import com.qczl.pme.service.ChannelService;
import com.qczl.pme.service.StbService;
import com.qczl.pme.vo.ChannelVo;

/**
 * 栏目控制器
 * @author SongR
 *
 */
@Controller
@RequestMapping(value="/stb/channel")
public class StbChannelController extends BaseController{
	Logger log = Logger.getLogger(this.getClass());
	@Autowired
	ChannelService channelservice;
	@Autowired
	StbService stbservice;
	
	/**
	 * 机顶盒launcher跳转
	 * @return
	 */
	@RequestMapping(value = "/goto/{key}/v1")
	public String gotoV1(HttpSession session, HttpServletRequest request, HttpServletResponse response,@PathVariable String key,String stbno){
		String link = "/404";
		try {
			ChannelVo vo = channelservice.getByKey(key);
			if (vo != null) {
				PMEChannel channel = new PMEChannel();
				link = vo.getLink();
				if(link.indexOf("?")==-1){
					link+="?stbno="+stbno+"&key="+key;
				}else{
					link+="&stbno="+stbno+"&key="+key;
				}
				vo.setVisits((vo.getVisits()==null?0:vo.getVisits())+ 1);
				BeanUtils.copyProperties(vo, channel);
				channelservice.update(channel);
			}
		} catch (Exception e) {
			link="/404";					
			log.error("跳转异常！",e);
		}
		return "redirect:" + link;
	}
	
	
	
	/**
	 * 获取顶级栏目
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="/top/v1",method={RequestMethod.GET,RequestMethod.POST})
	public Json getTop(HttpSession session, HttpServletRequest request, HttpServletResponse response,int start,int size){
		Json j = new Json();
		try {
			List<ChannelVo> vos = channelservice.getTop(start,size);
			if(vos.size()>0){
				j.setMsg("成功获取顶级栏目！");
				j.setResult(vos);
				j.setLength(new Long(vos.size()));
				j.setSize(channelservice.countTop());
			}else{
				j.setMsg("暂无顶级栏目！");
			}
		} catch (Exception e) {
			j.setMsg("获取顶级栏目接口异常！");
			j.setSuccess(false);
			j.setState(500);
			log.error("获取顶级栏目异常！",e);
		}
		return j;
	}
	
	/**
	 * 获取栏目
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="/get/v1",method={RequestMethod.GET,RequestMethod.POST})
	public Json getV1(String key){
		Json j = new Json();
		try {
			ChannelVo vo = channelservice.getByKey(key);
			if(null!=vo){
				j.setMsg("成功获取栏目信息！");
				j.setResult(vo);
			}else{
				j.setMsg("无此栏目！");
			}
		} catch (Exception e) {
			j.setMsg("获取栏目接口异常！");
			j.setSuccess(false);
			j.setState(500);
			log.error("获取栏目信息异常！",e);
		}
		return j;
	}
	
	
	/**
	 * 获取子级栏目列表
	 * @param key
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="/child/v1",method={RequestMethod.GET,RequestMethod.POST})
	public Json getChild(String key,int start,int size){
		Json j = new Json();
		try {
			List<ChannelVo> vos = channelservice.getChild(key,start,size);
			if(vos.size()>0){
				j.setMsg("成功获取子级栏目！");
				j.setResult(vos);
				j.setLength(new Long(vos.size()));
				j.setSize(channelservice.countChild(key));
			}else{
				j.setMsg("暂无子级栏目！");
			}
		} catch (Exception e) {
			j.setMsg("获取子级栏目接口异常！");
			j.setSuccess(false);
			j.setState(500);
			log.error("获取子级栏目信息异常！",e);
		}
		return j;
	}
	
	
	
}
